<?php
namespace App\Service;

use GuzzleHttp\Client;
use Hyperf\Utils\ApplicationContext;
use Hyperf\DbConnection\Db;
use Hyperf\Di\Annotation\Inject;

class CommicService
{
    public function __construct()
    {
        $container = ApplicationContext::getContainer();
        $this->redisService = $container->get(\Hyperf\Redis\Redis::class);
        $this->httpclient = new Client();
    }

    public function object_to_array($obj){
        # 对象列表装好为空
        $new_array = [];
        if ($obj){
            foreach ($obj as $key=>$value){
                $new_array[$key] = (array)$value;
            }
        }
        return $new_array;
    }

    public function object_to_find_array($obj){
        return (array)$obj;
    }
    public function get_template($spread_id=0,$ispay=0) {
        # 充值模版
        if($spread_id ==0) {
            $template_info  = Db::table("recharge_template")->where(["is_default"=>1])->first();
        } else {
            #获取推广连接的充值模版
            $template =  Db::table("data_spread")->where(["id"=>$spread_id])->first();
            if($ispay) {
                $template_id = isset($template->assistant_recharge_template_id) ?$template->assistant_recharge_template_id :0;
            } else {
                $template_id = isset($template->recharge_template_id) ? $template->recharge_template_id : 0;
            }
            if($template_id != 0) {
                $template_info  = Db::table("recharge_template")->where(["id"=>$template_id])->first();
                if(empty($template_info)) {
                    #默认值
                    $template_info  = Db::table("recharge_template")->where(["is_default"=>1])->first();
                }
            } else {
                $template_info  = Db::table("recharge_template")->where(["is_default"=>1])->first();
            }
        }
        return $template_info;
    }
//    public function get_template($applet_id=20000,$spread_id=0) {
//        # 充值模版
//        $template_infos  = Db::table("recharge_template")->where(["is_default"=>1])->first();
//        if($spread_id !=0) {
//            #获取推广连接的充值模版
//            $template =  Db::table("data_spread")->where(["id"=>$spread_id])->first();
//            $template_id = isset($template->recharge_template_id) ? $template->recharge_template_id : 0;
//            if($template_id != 0) {
//                $template_infos  = Db::table("recharge_template")->where(["id"=>$template_id])->first();
//            }
//        }
//        return $template_infos;
//    }

    public function get_applet_info(string $applet_id){
        # 获取小程序基本配置
        $redis_key = "applet:config";
        $applet_info = $this->redisService->hGet($redis_key,$applet_id);
        $applet_info = json_decode($applet_info,true);
//        $applet_info = [];
        if (empty($applet_info)){
            # 获取小程序基本信息
            $applet_info = Db::table("applet")->where([
                "id"=>$applet_id
            ])->first();
            $applet_info = $this->object_to_find_array($applet_info);
            $this->redisService->hSet($redis_key,$applet_id,json_encode($applet_info));
            $this->redisService->expire($redis_key,7200);
        }
        return $applet_info;
    }

    public function get_applet_playlet_rel($applet_id){
        # 获取小程序基本配置
        $redis_key = "applet:playlet_rel";
        $applet_ids = $this->redisService->hGet($redis_key,$applet_id);
        $applet_ids = json_decode($applet_ids,true);
        if (empty($applet_ids)){
            $applet_ids = [];
            # 获取小程序基本信息
            $playlet_id_ids = Db::table("applet_playlet_rel")->where([
                "applet_id"=>$applet_id,
                "status"=>1,
                "deleted"=>0,
            ])->select("playlet_id")->get();
            $playlet_id_ids = $this->object_to_array($playlet_id_ids);
            foreach ($playlet_id_ids as $key=>$val){
                $applet_ids[$key] = $val["playlet_id"];
            }
            $this->redisService->hSet($redis_key,$applet_id,json_encode($applet_ids));
        }
        return $applet_ids;
    }
    public function get_sys_config(){
        $redis_key = "data:system:config";
        $system_config_info = $this->redisService->get($redis_key);
        $system_config_info = json_decode($system_config_info,true);
        if (empty($system_config_info)){
            # 获取小程序基本信息
            $system_config_info = Db::table("data_system_config")->where(["id"=>1])->first();
            $system_config_info = $this->object_to_find_array($system_config_info);
            $this->redisService->set($redis_key,json_encode($system_config_info));
        }
        return $system_config_info;
    }

    public function get_curl_tool($url){
        $sys_config_info = $this->get_sys_config();
        if ($url){
            if (empty(strstr($url , 'http'))) {
                $url = $sys_config_info["static_base_url"].$url;
            }
        }
        return $url;
    }

    public function get_tday_exp_time(){
        # 当天剩余时间戳
        $day_exp_time = 86400 - (time() + 28800) % 86400;
        return $day_exp_time;
    }

    public function get_exp_time($number){
        echo "数量：".$number.PHP_EOL;
        $day_exp_time = $this->get_tday_exp_time();
        $stime = $number * 86400;
        return $day_exp_time+$stime;
    }

    public function get_recharge_name($id){
        $rec_data = [
            "1"=>"购买金币",
            "2"=>"购买vip",
            "3"=>"签到奖励",
            "4"=>"充值赠送",
            "5"=>"任务奖励",
        ];
        return $rec_data[$id];
    }
    public function add_user_log($data){
        $data["stype"] = 1;
        if (empty($data["name"])){
            $data["name"] = $this->get_recharge_name($data["recharge_type"]);
        }
        co(function () use ($data) {
//            print_r($data).PHP_EOL;
            echo "充值log".PHP_EOL;
            Db::table("data_user_log")->insertGetId($data);
        });
    }

    public function numbers_to_wang($number){
        #转换
        $resc =   $number>=10000 ? $number/10000 : $number;
        if (strstr($resc,".")){
            $ct = number_format($resc,1);
            return $ct."万";
        }else{
            return $resc;
        }
    }


    function array_fast_unique($array) {
        $temp = array();
        foreach ($array as $key => $value) {
            // 使用值作为键名，键名相同时覆盖原有值
            $temp[$value] = $value;
        }
        // 重置数组键名
        return array_values($temp);
    }
}